import Vue from 'vue'
import Router from 'vue-router'

// 导入对应的vuex文件
import store from './../store/store'

Vue.use(Router)


const router =new Router({
     mode:'history',
     routes: [{
       path: '/',
       name: "欢迎",
       meta: {
                requireAuth: false,  // 添加该字段，表示进入这个路由是需要登录的
            },
       component: resolve => require(["./../views/index/Index.vue"], resolve)
     },{
       path: '/login',
       name: "登录",
       meta: {
                requireAuth: false,
            },
       component: resolve => require(["./../views/login/Login.vue"], resolve)
     },{
       path: '/regist',
       name: "注册",
       meta: {
                requireAuth: false,
            },
       component: resolve => require(["./../views/login/Regist.vue"], resolve)
     },{
       path: '/home',
       name: "注册",
       meta: {
                requireAuth: true,
            },
       component: resolve => require(["./../views/index/Three.vue"], resolve)
     }]
})


router.beforeEach((to, from, next) => {
    if (to.meta.requireAuth) {  // 判断该路由是否需要登录权限
      console.log(store.state.user);
        if (store.state.user) {  // 通过vuex state获取当前的token是否存在
            next();
        }
        else {
          console.log(store.state.user);
            next({
                path: '/login',
                query: {redirect: to.fullPath}  // 将跳转的路由path作为参数，登录成功后跳转到该路由
            })
        }
    }
    else {
        next();
    }
})

export default router
// 导入相应的子组件
// import Hello from './../components/Hello'
// import Login from './../components/Login'
// import THREE from './../'
// Vue.use(Router)
// var router = new Router({
//   mode: 'history',
//   routes: [
//     { name: 'hello', path: '/', component: Hello},
//     {name:'login',path:'/login',component:Login}
//   ]
// })
